﻿using System;
using System.Windows.Input;

using Perseus;

namespace Hetymine.Executioner.Data {
    public class HotKeyInfo {
        private string _KeyValue;
        private Key _Key;

        public HotKeyInfo() {
            this.Key = Key.None;
            this.Modifiers = ModifierKeys.None;
        }
        public HotKeyInfo(string hotKey) {
            this.HotKey = hotKey;            
        }
        public HotKeyInfo(Key key, ModifierKeys modifiers) {
            this.Key = key;
            this.Modifiers = modifiers;
        }

        public string HotKey {
            get {
                string hotKey = string.Empty;

                if ((this.Modifiers | ModifierKeys.Control) == this.Modifiers) {
                    hotKey += "Ctrl + ";
                }

                if ((this.Modifiers | ModifierKeys.Shift) == this.Modifiers) {
                    hotKey += "Shift + ";
                }

                if ((this.Modifiers | ModifierKeys.Alt) == this.Modifiers) {
                    hotKey += "Alt + ";
                }

                hotKey += this.KeyValue;

                return hotKey;
            }
            set {
                string[] parts = value.Split(
                    "+",
                    SplitOptions.Trim | SplitOptions.RemoveEmptyEntries
                );

                this.Key = Key.None;
                this.Modifiers = ModifierKeys.None;

                foreach (string part in parts) {
                    Key key = PInput.ResolveKey(part);

                    if (key == Key.LeftCtrl || key == Key.RightCtrl) {
                        this.Modifiers |= ModifierKeys.Control;
                    }
                    else if (key == Key.LeftShift || key == Key.RightShift) {
                        this.Modifiers |= ModifierKeys.Shift;
                    }
                    else if (key == Key.LeftAlt || key == Key.RightAlt) {
                        this.Modifiers |= ModifierKeys.Alt;
                    }
                    //else if (key == Key.LWin || key == Key.RWin) {
                    //    this.Modifiers |= ModifierKeys.Windows;                    
                    //}
                    else {
                        this._Key = key;
                        this._KeyValue = part;
                    }
                }
            }
        }

        public string KeyValue {
            get { return this._KeyValue; }
            set { 
                this._KeyValue = value;
                this._Key = PInput.ResolveKey(value);
            }
        }
        public Key Key {
            get { return this._Key; }
            set {
                this._Key = value;
                this._KeyValue = PInput.FormatKey(value);
            }
        }
        public ModifierKeys Modifiers { get; set; }

        /// <summary>
        /// Set to hotkey's global atom when registered.
        /// </summary>
        internal ushort Id { get; set; }
        /// <summary>
        /// Only need to register hotkey once even if multiple 
        /// commands use the same one.
        /// </summary>
        internal bool Registered { get; set; }        
    }
}
